<?php
/**
 * EventTrigger Data Accessor for CTRT_RaidTracker
 *
 * @category Plugins
 * @package CT_RaidTrackerImport
 * @copyright (c) 2006, EQdkp <http://www.eqdkp.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @author Garrett Hunter <loganfive@blacktower.com>
 * $Rev: 84 $ $Date: 2007-04-22 09:42:16 +0000 (Sun, 22 Apr 2007) $
 */

if ( !defined('EQDKP_INC') )
{
    die('Haxor!!!');
}

include_once('dataaccessor.php');

/**
 * CRUD Class EventTrigger table
 * @subpackage CTRTDataAccessors
 */
class CTRT_EventTrigger extends CTRT_DataAccessor {

	var $_event_trigger_name;
	var $_event_trigger_result;
	
	function CTRT_EventTrigger () { 

		$this->setMyTable(CTRT_EVENT_TRIGGERS_TABLE);
		$this->setMyPrimaryKey("event_trigger_id");

	}
	
	/**
	 * Update an alias record
	 * @var string $alias
	 * @var integer $member_id
	 */
	function update($data) {

		$this->setEventTriggerName($data['event_trigger_name']);
		$this->setEventTriggerResult($data['event_trigger_result']);

		if (!$this->isDuplicate($data['event_trigger_id'],$this->getEventTriggerName(),$this->getEventTriggerResult())) {

			$this->doUpdate ($data[$this->getMyPrimaryKey()],
			                 array('event_trigger_name'=>stripslashes($this->getEventTriggerName()),
							       'event_trigger_result'=>stripslashes($this->getEventTriggerResult())
								  ));
		} else {
			return 0;
		}

		return 1;
	}

	/**
	 * Insert an event trigger record
	 * @var array $data Contains all column data to insert
	 */
	function insert($data) {
		global $db;

		$this->setEventTriggerName($data['event_trigger_name']);
		$this->setEventTriggerResult($data['event_trigger_result']);

		if (!$this->isDuplicate(-1,$this->getEventTriggerName(),$this->getEventTriggerResult())) {
		
			$this->doInsert (array('event_trigger_name'=>stripslashes($this->getEventTriggerName()),
			                       'event_trigger_result'=>stripslashes($this->getEventTriggerResult())));
		} else {
			return 0;
		}
		
		return 1;
	}

	/**
	 * Get all alias & member name pairs
	 */
	function getRecord ($cmd=CTRT_GET_ALL,$param="") {
        global $db;

        /**
         * Generate the list of eventtriggers
         */
        $sql =   "SELECT event_trigger_id, event_trigger_name, event_trigger_result
                    FROM " . $this->getMyTable() . " ";
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_ID:
				$sql .= "WHERE event_trigger_id = ".$param." ";
				break;
					
			case CTRT_GET_BY_NAME:
				$this->setEventTriggerName($param);
				$sql .= "WHERE event_trigger_name = '".mysql_real_escape_string($this->getEventTriggerName())."' ";
				break;
			
			default: break;
		}
		$sql .= "ORDER BY event_trigger_result";
		
        $result = $db->query($sql);

		$eventtriggers = array();
        while ( $row = $db->fetch_record($result) )
        {
			$eventtriggers[] = array("event_trigger_id"=>$row["event_trigger_id"],
			                         "event_trigger_name"=>$row["event_trigger_name"],
								     "event_trigger_result"=>$row["event_trigger_result"]
								    );
        }
        $db->free_result($result);
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_NAME:
			case CTRT_GET_BY_ID:
				$eventtriggers = $eventtriggers[0];
				break;
			
			default: break;
		}
		
		return $eventtriggers;
	}
	
	/**
	 * Checks a name / result combo against database for duplicate
	 * @var int $key primary key used to filter this record out
	 * @var string $name event trigger name
	 * @var string $result event trigger result
	 */
	function isDuplicate($key, $name, $result) {
		global $db;
		
		/**
		 * Only one name <-> result combination can exist
		 */ 
		$sql = "SELECT event_trigger_id
		          FROM ".$this->getMyTable()."
				 WHERE event_trigger_name = '".mysql_real_escape_string($name)."'
				   AND event_trigger_result='".mysql_real_escape_string($result)."'
				   AND event_trigger_id != ".$key;
		$record = $db->query_first($sql);
		if (isset($record)) {		
			return 1;
		} else {
			return 0;
		}
	}
	
	//========================================
	// Getter / Setter methods
	//========================================

	/**
	 * Get / Set event_trigger_name
	 */
	function setEventTriggerName($name) { $this->_event_trigger_name = $this->capitalize($name); return 1; }
	function getEventTriggerName() { return $this->_event_trigger_name; }

	/**
	 * Get / Set event_trigger_result
	 */
	function setEventTriggerResult($result) { $this->_event_trigger_result = $this->capitalize($result); return 1; }
	function getEventTriggerResult() { return $this->_event_trigger_result; }
	
	
}
?>